<template>
    <div class="contact-container">
        <!-- 导航栏 -->
        <TridentCustomNav>
            <template #default>通讯录</template>
        </TridentCustomNav>

        <!-- 内容区域开始 -->
        <div class="content-wrap">
            <!-- 联系人区域开始 -->
            <div class="contact-wrap">
                <!-- 通讯菜单区域开始 -->
                <div class="contact-menus">
                    <div class="item" v-for="(item, index) in contactMenus" :key="index" @click="$router.push(item.url)">
                        <div class="image-box">
                            <i :class="`iconfont icon-${item.icon}`"></i>
                            <div class="mark" v-if="index == 0 && commonStore.friendApplyTotal > 0">{{ commonStore.friendApplyTotal }}</div>
                        </div>
                        <div class="title-box">{{ item.title }}</div>
                    </div>
                </div>
                <!-- 通讯菜单区域结束 -->

                <!-- 星标朋友区域开始 -->
                <div class="star-contact-items">
                    <div class="title-panel">星标朋友</div>
                    <!-- 骨架屏区域 -->
                    <TridentCellSkeleton
                        v-if="isSkeleton"
                        v-for="(index) in 7" :key="index"
                        :isLeftSecondRow="false"
                        :isRight="false"
                        width="50px"
                        height="50px"
                    />

                    <div class="item" v-for="(item, index) in starFriendList" :key="index" @click="$router.push('/friendDetail?id='+ item.id)">
                        <div class="image-box">
                            <van-image
                                width="45"
                                height="45"
                                :src="item.info.avatar_url ? global.MINIO_FILE_DOMAIN + item.info.avatar_url : 'src/assets/images/default_avatar.png'"
                                fit="contain"
                            />
                        </div>
                        <div class="title-box">{{item.info.username}}</div>
                    </div>
                </div>
                <!-- 星标朋友区域结束 -->

                <!-- 联系人区域开始 -->
                <van-index-bar
                    :sticky-offset-top="45"
                    highlight-color="#27BA9B"
                    :sticky="true"
                    :index-list="Object.keys(generalFriendList)"
                >
                    <div class="contact-box" v-for="(key, index) in Object.keys(generalFriendList)" :key="index">
                        <van-index-anchor :index="key"/>
                        <div class="contact-item" v-for="(value, index) in generalFriendList[key]" :key="index" @click="$router.push('/friendDetail?id='+ value.id)">
                            <div class="image-box">
                                <van-image
                                    width="50"
                                    height="50"
                                    :src="value.info.avatar_url ? global.MINIO_FILE_DOMAIN + value.info.avatar_url : 'src/assets/images/default_avatar.png'"
                                    fit="contain"
                                />
                            </div>
                            <div class="title-box">{{value.info.username}}</div>
                        </div>
                    </div>
                </van-index-bar>
                <!-- 联系人区域结束 -->

                <!-- 返回顶部区域开始 -->
                <van-back-top bottom="100" right="10"><i class="iconfont icon-top" style="font-size: 30px;"></i></van-back-top>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reqFriendListAPI } from '@/apis/friend'
import { useCommonStore } from '@/store/modules/common'
import { useUserStore } from '@/store/modules/user'
import type { Friend, FriendListParams } from '@/types/friend'
import { pinyin } from 'pinyin-pro'
import { inject, onMounted, reactive, ref } from 'vue'



// --------------- 定义全局变量开始 ---------------
// 全局属性
const global: any = inject('global')

// 用户存储
const userStore = useUserStore()

// 骨架屏
const isSkeleton = ref<boolean>(false)

// 通讯菜单
const contactMenus = reactive([
    {
        title: '新的朋友',
        icon: 'xinpengyou',
        url: '/newFriend'
    },
    {
        title: '仅聊天好友',
        icon: 'a-zu17795',
        url: '/onlyChatFriend'
    },
    {
        title: '群聊',
        icon: 'qunliao',
        url: '/groupChat'
    },
    {
        title: '标签',
        icon: 'biaoqian',
        url: '/label'
    },
    {
        title: '通知',
        icon: 'tongzhi',
        url: '/notify'
    },
    {
        title: '黑名单',
        icon: 'heimingdan',
        url: '/blacklist'
    }
])

// 公共存储
const commonStore = useCommonStore()
// --------------- 定义全局变量结束 ---------------



onMounted(async() => {
    await getFriendList()
})



// --------------- 处理好友列表开始 ---------------
const params = ref<FriendListParams>({
    is_my_friend: 1,
    is_friend_info: 1,
    status: '1',
    page: 1,
    page_size: -1
})

// 星标好友列表
const starFriendList = ref<Friend[]>([])

// 普通好友列表
const generalFriendList: any = ref({})

// 获取好友列表
const getFriendList = async () => {
    isSkeleton.value = true
    const result = await reqFriendListAPI(params.value)
    let tempFriendList: any = {}
    result.data.friends.forEach(item => {
        let is_asterisk = ''
        item.information.forEach(value => {
            if (value.uid != userStore.profile.user.uid) {
                is_asterisk = value.is_asterisk
            }
        })

        if (is_asterisk == '1') {
            starFriendList.value.push(item)
        } else  {
            let initial = pinyin(item.info.username, {pattern: 'first', toneType: 'none' }).substr(0, 1).toLocaleUpperCase()
            
            if (!tempFriendList[initial]) {
                tempFriendList[initial] = []
            }

            tempFriendList[initial].push(item)
        }
    })

    generalFriendList.value = Object.keys(tempFriendList).sort().reduce((acc: any, key) => {
        acc[key] = tempFriendList[key];
        return acc;
    }, {});
    isSkeleton.value = false
}
// --------------- 处理好友列表结束 ---------------
</script>

<style scoped lang="scss">
page,
.contact-container {
    height: 100vh;

    // 内容区域开始
    .content-wrap {
        padding: 55px 0;
        .contact-wrap {
            // height: calc(100vh - 100px);
            // overflow: hidden;
            padding: 0 5px;

            // 通讯菜单区域开始
            .contact-menus {
                padding: 0 15px;
                .item {
                    display: flex;
                    align-items: center;

                    // 群聊
                    &:nth-child(3) .image-box {
                        background-color: #07C160;
                        .iconfont {
                            font-size: 35px;
                        }
                    }

                    // 标签
                    &:nth-child(4) .image-box {
                        background-color: #2781D7;
                        .iconfont {
                            font-size: 28px;
                        }
                    }

                    // 通知
                    &:nth-child(5) .image-box {
                        background-color: #56ACF4;
                        .iconfont {
                            font-size: 28px;
                        }
                    }

                    // 黑名单
                    &:nth-child(6) .image-box {
                        background-color: #c7c6c6;
                        .iconfont {
                            font-size: 28px;
                        }
                    }
                    .image-box {
                        background-color: #FA9D3D;
                        width: 50px;
                        height: 50px;
                        line-height: 50px;
                        border-radius: 5px;
                        text-align: center;
                        position: relative;

                        .iconfont {
                            font-size: 25px;
                            color: #fff;
                        }

                        .mark {
                            background-color: red;
                            position: absolute;
                            width: 16px;
                            height: 16px;
                            line-height: 16px;
                            border-radius: 8px;
                            top: 0px;
                            right: 0px;
                            color: #fff;
                            text-align: center;
                            font-size: 10px;
                        }
                    }
                    .title-box {
                        flex: 1;
                        margin-left: 15px;
                        color: #000;
                        font-size: 18px;
                        border-bottom: 1px solid #EDEDED;
                        padding: 25px 0;
                    }
                }
            }
            // 通讯菜单区域结束

            // 星标联系人区域开始
            .star-contact-items {
                .title-panel {
                    height: 34px;
                    line-height: 34px;
                    background-color: #EDEDED;
                    font-size: 12px;
                    padding-left: 15px;
                }
                .item {
                    padding: 0 15px;
                    display: flex;
                    align-items: center;
                    .image-box {
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        width: 50px;
                        height: 50px;
                        border-radius: 5px;
                        border: 1px solid #EDEDED;
                    }
                    .title-box {
                        flex: 1;
                        margin-left: 15px;
                        color: #000;
                        font-size: 18px;
                        border-bottom: 1px solid #EDEDED;
                        padding: 25px 0;
                    }
                }
            }
            // 星标联系人区域结束

            // 联系人区域开始
            .contact-box {
                margin: 10px 0;
                .contact-item {
                    display: flex;
                    align-items: center;
                    padding-left: 15px;
                    background-color: #fff;
                    &:last-child .title-box {
                        border-bottom: none
                    }
                    .image-box {
                        width: 50px;
                        height: 50px;
                        line-height: 50px;
                        border-radius: 5px;
                        text-align: center;
                        overflow: hidden;
                        border: 1px solid #EDEDED;
                    }
                    .title-box {
                        flex: 1;
                        margin-left: 15px;
                        color: #000;
                        font-size: 18px;
                        border-bottom: 1px solid #EDEDED;
                        padding: 25px 0;
                    }
                }
            }
            // 联系人区域结束
        }
    }
}
</style>